import { UPDATE_PRUDUCTS, UPDATE_SKU } from '../mutations-type'
import { getProductsList } from '../../services/cart'

export default {
  namespaced: true,
  state: {
    products: []
  },
  getters: {

  },
  mutations: {
    [UPDATE_PRUDUCTS] (state: { products: any; }, payload: any) {
      state.products = payload
    },
    [UPDATE_SKU] (state: { products: { find: (arg0: (item: any) => boolean) => { sku: any; }; }; }, { id, sku }: any) {
      state.products.find(item => item.id === id).sku = sku
      console.log(id, sku)
    }
  },
  actions: {
    // @ts-ignore
    async getProducts ({ commit }, payload) {
      // @ts-ignore
      const { data } = await getProductsList(payload)
      console.log('数据访问成功')
      commit(UPDATE_PRUDUCTS, data)
    }
  }
}
